package cn.itcast.dao;

import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.itcast.vo.Customer;
import cn.itcast.vo.PageBean;
import lombok.Cleanup;

public class MybatisCustomerDaoImpl implements CustomerDao {
	
	private static SqlSessionFactory sqlSessionFactory = null;

	@Override
	public int save(Customer paramCustomer) {

		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			int result = customerMapper.save(paramCustomer);
			sqlSession.commit();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<Customer> findAll() {
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			return customerMapper.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int delete(String paramString) {
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			int result = customerMapper.delete(paramString);
			sqlSession.commit();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Customer findById(String paramString) {
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			return customerMapper.findById(paramString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int update(Customer paramCustomer) {
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			int result = customerMapper.update(paramCustomer);
			sqlSession.commit();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<Customer> findByWhere(String paramString1, String paramString2) {
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			return customerMapper.findByWhere(paramString1, paramString2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PageBean<Customer> findByPage(int paramInt1, int paramInt2) {
		PageBean<Customer> pb = new PageBean<Customer>();
		PageHelper.startPage(paramInt1, paramInt2);
		
		try {
			@Cleanup SqlSession sqlSession = getSqlSessionFactory().openSession();
			CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);
			List<Customer> list = customerMapper.findAll();
			pb.setBeanList(list);
			pb.setTotalCount((int) ((Page<?>) list).getTotal());
			pb.setPageCode(paramInt1);
			pb.setPageSize(paramInt2);
			return pb;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static SqlSessionFactory getSqlSessionFactory() {
		if (sqlSessionFactory == null) {
			try {
				String resource = "mybatis-config.xml";
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return sqlSessionFactory;
	}
}
